package com.java.example.network

import android.annotation.SuppressLint
import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import java.security.KeyStore
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

/**
 * @author chenll
 * @date 2024/2/22
 */

class EasySSLSocketFactory(truststore: KeyStore?) : SSLSocketFactory() {
    private var sslContext: SSLContext? = null

    @SuppressLint("TrustAllX509TrustManager", "CustomX509TrustManager")
    inner class SSLTrustAllManager : X509TrustManager {
        @Throws(CertificateException::class)
        override fun checkClientTrusted(arg0: Array<X509Certificate>, arg1: String) {
        }

        @Throws(CertificateException::class)
        override fun checkServerTrusted(arg0: Array<X509Certificate>, arg1: String) {
        }

        override fun getAcceptedIssuers(): Array<X509Certificate>? {
            return null
        }
    }

    init {
        try {
            sslContext = SSLContext.getInstance("TLS")
            sslContext!!.init(
                null, arrayOf<TrustManager>(SSLTrustAllManager()),
                null
            )
        } catch (_: Exception) {
        }
    }

    override fun getDefaultCipherSuites(): Array<String>? {
        return null
    }

    override fun getSupportedCipherSuites(): Array<String>? {
        return null
    }

    @Throws(IOException::class)
    override fun createSocket(): Socket {
        return sslContext!!.socketFactory.createSocket()
    }

    @Throws(IOException::class)
    override fun createSocket(socket: Socket, host: String, port: Int, autoClose: Boolean): Socket {
        return sslContext!!.socketFactory.createSocket(
            socket, host, port,
            autoClose
        )
    }

    @Throws(IOException::class, UnknownHostException::class)
    override fun createSocket(host: String, port: Int): Socket {
        return sslContext!!.socketFactory.createSocket(host, port)
    }

    @Throws(IOException::class, UnknownHostException::class)
    override fun createSocket(host: String, port: Int, localHost: InetAddress, localPort: Int): Socket {
        return sslContext!!.socketFactory.createSocket(
            host, port, localHost,
            localPort
        )
    }

    @Throws(IOException::class)
    override fun createSocket(host: InetAddress, port: Int): Socket {
        return sslContext!!.socketFactory.createSocket(host, port)
    }

    @Throws(IOException::class)
    override fun createSocket(address: InetAddress, port: Int, localAddress: InetAddress, localPort: Int): Socket {
        return sslContext!!.socketFactory.createSocket(
            address, port, localAddress,
            localPort
        )
    }

    companion object {
        private const val TAG = "EasySSLSocketFactory"
    }
}
